<?php

namespace app\index\model;

use think\Model;

class Mood extends Model
{
    protected $dateFormat = FALSE;

    /**
     * 获取所有针对文章的评论。
     */
    public function messages()
    {
        return $this->morphMany('Message', 'messagetable', 2);
    }


    public static function mAll($page = 1, $limit = 1, $map = [])
    {
        $rel = self::withCount('messages')->paginate($limit, FALSE, [
            'page' => $page,
            'query' => [$map],
        ])->toArray();

        return $rel;
    }


    public static function read($id = 0, $page = 1, $limit = 1)
    {

        $rel = self::get($id);
        $data = $rel->toArray();

        //多态关联分页查询
        $data['messages'] = $rel->messages()->order('id', 'desc')->paginate($limit, FALSE, [
            'page' => $page,
        ])->toArray();


//        halt($data);

        return $data;
    }
}
